from pdfminer.high_level import extract_text  

def extract_ec(pdf_path=''):
    # 使用 pdfminer.six 提取文本  
    text = extract_text(pdf_path)
    return text  


def append_to_txt(text, txt_path):
    # 将提取的文本写入 txt 文件
    with open(txt_path, 'a', encoding='utf-8') as f:
        f.write(text)
    print('Text saved to', txt_path)





if __name__ == '__main__':
    txt_path = f'CHS-DRG1.2-2\\CHS-DRG1.2_EC.TXT'
    pdf_files=[f'CHS-DRG1.2-2\\CHS-DRG_1481-1580.pdf',f'CHS-DRG1.2-2\\CHS-DRG_1581-1680.pdf',f'CHS-DRG1.2-2\\CHS-DRG_1681-1780.pdf',f'CHS-DRG1.2-2\\CHS-DRG_1781-1825.pdf']
    for pdf_path in pdf_files:
        text = extract_ec(pdf_path)
        append_to_txt(text, txt_path)
        append_to_txt('\n'+'='*40+'\n',txt_path)
        print(f'Extracted text from {pdf_path}')
